﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Biblioteca.Utils;
using CDC;
using System.Collections.Generic;
using System.Text;
using System.Data.OracleClient;
using Pms.Utils;


namespace SERIX
{
    public partial class PreencherQuestionario : Seguranca
    {
        private CQuestionarios quest;
        private string _idt_qst;
        private string _op;

        protected void Page_Load(object sender, EventArgs e)
        {
            base.OnLoad(e);

            if (Request["OP"] == null)
            {
                Response.Redirect("~/Logged.aspx");
            }
            _op = Request["OP"];

            if (Request["idt_qst"] != null)
            {
                _idt_qst = Server.UrlDecode(Criptografia.Descriptografar(Request["idt_qst"]));
                quest = CQuestionarios.PesquisaporCodigo(_idt_qst, UsuAut.Login);
                lbQuestionario.Text = CTiposQuestionarios.PesquisaporCodigo(quest.Idt_tiq, UsuAut.Login).Dsc_tiq;
                lbPropriedade.Text = CPropriedades.PesquisaporCodigo(quest.Idt_prop, UsuAut.Login, false).Nome_prop;
                lbBeneficario.Text = CBeneficiarios.PesquisaporCodigo(quest.Idt_benef, UsuAut.Login, false).Nom_benef;
                MontarQuestionario(Convert.ToInt32(_idt_qst));
                ObterRespostas(Convert.ToInt32(_idt_qst));
                if (_permissao.Equals(Constantes.PERM_CONSULTA) || _op.Equals("C"))
                {
                    DesligarControles(Convert.ToInt32(_idt_qst));
                }
                if (quest.Idt_qst == null)
                {
                    Response.Redirect("~/Logged.aspx");
                }
            }
            else
            {
                Response.Redirect("~/Logged.aspx");
            }
        }

        public void DesligarControles(int idt_qst)
        {
            DataTable dtCategorias = CCategoriasQuestionarios.ObterCategoriasPorQuestionario(idt_qst.ToString(), UsuAut.Login);
            foreach (DataRow linha_cat in dtCategorias.Rows)
            {
                DataTable dtQuestoes = CQuestoes.ObterQuestoesPorCategoria(linha_cat["idt_tiq"].ToString(), linha_cat["idt_cat"].ToString(), UsuAut.Login);
                foreach (DataRow linha_quest in dtQuestoes.Rows)
                {
                    CQuestoes questao = CQuestoes.PesquisaporCodigo(linha_quest["idt_tiq"].ToString(), linha_quest["idt_cat"].ToString(), linha_quest["idt_que"].ToString(), UsuAut.Login);
                    string idt_tiq = linha_quest["idt_tiq"].ToString();
                    string idt_cat = linha_quest["idt_cat"].ToString();
                    string idt_que = linha_quest["idt_que"].ToString();
                    string idq = idt_tiq + '_' + idt_cat + '_' + idt_que;

                    switch (questao.Tpo_que)
                    {
                        case "A":
                            {
                                TextBox tb = (TextBox)cdc.FindControlRecursive(Page.Master, "tb_" + idq);
                                tb.Enabled = false;
                                break;
                            }
                        case "D":
                            {
                                CheckBoxList cbl = (CheckBoxList)cdc.FindControlRecursive(Page.Master, "cbl_" + idq);
                                cbl.Enabled = false;
                                break;
                            }
                        case "U":
                            {
                                RadioButtonList rbl = (RadioButtonList)cdc.FindControlRecursive(Page.Master, "rbl_" + idq);
                                rbl.Enabled = false;
                                break;
                            }
                        case "C":
                            {
                                DropDownList ddl = (DropDownList)cdc.FindControlRecursive(Page.Master, "ddl_" + idq);
                                ddl.Enabled = false;
                                break;
                            }
                        default:
                            break;
                    }
                }
            }
        }

        public void MontarQuestionario(int idt_qst)
        {
            DataTable dtCategorias = CCategoriasQuestionarios.ObterCategoriasPorQuestionario(idt_qst.ToString(), UsuAut.Login);
            foreach (DataRow linha_cat in dtCategorias.Rows)
            {
                string id = linha_cat["idt_cat"].ToString();
                cdc.criarLabel(PanelPrincipal, 16, "lb_c" + id, linha_cat["ordem"].ToString() + ". " + linha_cat["dsc_cat"].ToString());
                cdc.AddRegua(PanelPrincipal);
                cdc.AddQuebradeLinha(PanelPrincipal);
                DataTable dtQuestoes = CQuestoes.ObterQuestoesPorCategoria(linha_cat["idt_tiq"].ToString(), linha_cat["idt_cat"].ToString(), UsuAut.Login);
                foreach (DataRow linha_quest in dtQuestoes.Rows)
                {
                    CQuestoes questao = CQuestoes.PesquisaporCodigo(linha_quest["idt_tiq"].ToString(), linha_quest["idt_cat"].ToString(), linha_quest["idt_que"].ToString(), UsuAut.Login);
                    string idt_tiq = linha_quest["idt_tiq"].ToString();
                    string idt_cat = linha_quest["idt_cat"].ToString();
                    string idt_que = linha_quest["idt_que"].ToString();
                    string idq = idt_tiq + '_' + idt_cat + '_' + idt_que;
                    string nro_quest = linha_cat["ordem"].ToString() + '.' + linha_quest["ordem"].ToString();
                    string dsc_quest = nro_quest + " - " + linha_quest["dsc_que"].ToString();
                    bool obrigatoria = linha_quest["resp_obr"].Equals("V");

                    DataTable dtOpcoes = COpcoesQuestoes.ObterOpcoesPorQuestao(idt_tiq, idt_cat, idt_que, UsuAut.Login);
                    List<ListItem> lista;
                    switch (questao.Tpo_que)
                    {
                        case "A":
                            {
                                cdc.criarQuestaoAberta(PanelPrincipal, idq, nro_quest, dsc_quest, obrigatoria);
                                break;
                            }
                        case "D":
                            {
                                if (questao.Tpo_vinc.Equals("S"))
                                {
                                    lista = cdc.montarLista(dtOpcoes, "SEQ_OPCAO", "DSC_OPCAO", false);
                                }
                                else
                                {
                                    lista = cdc.montarLista(dtOpcoes, "SEQ_OPCAO", "DESCRICAO", false);
                                }
                                cdc.criarQuestaoCheck(PanelPrincipal, idq, nro_quest, dsc_quest, lista, obrigatoria);
                                break;
                            }
                        case "U":
                            {
                                if (questao.Tpo_vinc.Equals("S"))
                                {
                                    lista = cdc.montarLista(dtOpcoes, "SEQ_OPCAO", "DSC_OPCAO", false);
                                }
                                else
                                {
                                    lista = cdc.montarLista(dtOpcoes, "SEQ_OPCAO", "DESCRICAO", false);
                                }
                                cdc.criarQuestaoRadio(PanelPrincipal, idq, nro_quest, dsc_quest, lista, obrigatoria);
                                break;
                            }
                        case "C":
                            {
                                if (questao.Tpo_vinc.Equals("S"))
                                {
                                    lista = cdc.montarLista(dtOpcoes, "SEQ_OPCAO", "DSC_OPCAO", true);
                                }
                                else
                                {
                                    lista = cdc.montarLista(dtOpcoes, "SEQ_OPCAO", "DESCRICAO", true);
                                }
                                cdc.criarQuestaoCombo(PanelPrincipal, idq, nro_quest, dsc_quest, lista, obrigatoria);
                                break;
                            }
                        default:
                            break;
                    }
                }
            }
            cdc.AddQuebradeLinha(PanelPrincipal);
            cdc.AddLiteral(PanelPrincipal, "<center>");
            cdc.criarBotao(PanelPrincipal, "btnOK", "OK", ClickOK);
            cdc.AddLiteral(PanelPrincipal, "&nbsp;&nbsp;");
            if (_op.Equals("U")) {
              cdc.criarBotao(PanelPrincipal, "btnCancelar", "Cancelar", ClickCancelar);
            }
            cdc.AddLiteral(PanelPrincipal, "</center>");
            cdc.CriarValidationSumary(PanelPrincipal);
        }

        public void ObterRespostas(int idt_qst)
        {
            DataTable dtCategorias = CCategoriasQuestionarios.ObterCategoriasPorQuestionario(idt_qst.ToString(), UsuAut.Login);
            foreach (DataRow linha_cat in dtCategorias.Rows)
            {
                DataTable dtQuestoes = CQuestoes.ObterQuestoesPorCategoria(linha_cat["idt_tiq"].ToString(), linha_cat["idt_cat"].ToString(), UsuAut.Login);
                foreach (DataRow linha_quest in dtQuestoes.Rows)
                {
                    CQuestoes questao = CQuestoes.PesquisaporCodigo(linha_quest["idt_tiq"].ToString(), linha_quest["idt_cat"].ToString(), linha_quest["idt_que"].ToString(), UsuAut.Login);
                    string idt_tiq = linha_quest["idt_tiq"].ToString();
                    string idt_cat = linha_quest["idt_cat"].ToString();
                    string idt_que = linha_quest["idt_que"].ToString();
                    string idq = idt_tiq + '_' + idt_cat + '_' + idt_que;

                    switch (questao.Tpo_que)
                    {
                        case "A":
                            {
                                TextBox tb = (TextBox)cdc.FindControlRecursive(Page.Master, "tb_" + idq);
                                CRespostasAbertas resp = CRespostasAbertas.PesquisaporCodigo(_idt_qst, idt_tiq, idt_cat, idt_que, UsuAut.Login);
                                tb.Text = resp.Resposta;
                                break;
                            }
                        case "D":
                            {
                                CheckBoxList cbl = (CheckBoxList)cdc.FindControlRecursive(Page.Master, "cbl_" + idq);
                                foreach (ListItem item in cbl.Items)
                                {
                                    item.Selected = CRespostasMultiplas.ExisteResposta(_idt_qst, idt_tiq, idt_cat, idt_que, item.Value, UsuAut.Login);
                                }
                                break;
                            }
                        case "U":
                            {
                                RadioButtonList rbl = (RadioButtonList)cdc.FindControlRecursive(Page.Master, "rbl_" + idq);
                                CRespostasMultiplas resp = CRespostasMultiplas.PesquisaporCodigo(_idt_qst, idt_tiq, idt_cat, idt_que, UsuAut.Login);
                                if (resp.Seq_opcao != null)
                                {
                                    rbl.SelectedValue = resp.Seq_opcao;
                                }
                                break;
                            }
                        case "C":
                            {
                                DropDownList ddl = (DropDownList)cdc.FindControlRecursive(Page.Master, "ddl_" + idq);
                                CRespostasMultiplas resp = CRespostasMultiplas.PesquisaporCodigo(_idt_qst, idt_tiq, idt_cat, idt_que, UsuAut.Login);
                                if (resp.Seq_opcao != null)
                                {
                                    ddl.SelectedValue = resp.Seq_opcao;
                                }
                                break;
                            }
                        default:
                            break;
                    }
                }
            }
        }

        protected void ClickCancelar(object sender, EventArgs e)
        {
            if (Session["RetornoQuest"] != null)
            {
                Response.Redirect(Session["RetornoQuest"].ToString());
            }
            else
            {
                Response.Redirect("~/Logged.aspx");
            }
        }
        protected void ClickOK(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            if (IsValid && _permissao.Equals(Constantes.PERM_MANUTENCAO) && _op.Equals("U"))
            {
                OracleCommand command = new OracleCommand();
                try
                {
                    command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, UsuAut.Login, BiblioPMS.Demo);
                    OracleTransaction trans = command.Connection.BeginTransaction();
                    command.Transaction = trans;

                    //Apagando Respostas Antigas
                    CRespostasAbertas.ExcluirTodasRespostas(_idt_qst, command);
                    CRespostasMultiplas.ExcluirTodasRespostas(_idt_qst, command);

                    //Obtendo respostas
                    DataTable dtCategorias = CCategoriasQuestionarios.ObterCategoriasPorQuestionario(_idt_qst.ToString(), UsuAut.Login);
                    foreach (DataRow linha_cat in dtCategorias.Rows)
                    {
                        DataTable dtQuestoes = CQuestoes.ObterQuestoesPorCategoria(linha_cat["idt_tiq"].ToString(), linha_cat["idt_cat"].ToString(), UsuAut.Login);
                        foreach (DataRow linha_quest in dtQuestoes.Rows)
                        {
                            CQuestoes questao = CQuestoes.PesquisaporCodigo(linha_quest["idt_tiq"].ToString(), linha_quest["idt_cat"].ToString(), linha_quest["idt_que"].ToString(), UsuAut.Login);
                            string idt_tiq = linha_quest["idt_tiq"].ToString();
                            string idt_cat = linha_quest["idt_cat"].ToString();
                            string idt_que = linha_quest["idt_que"].ToString();
                            string idq = idt_tiq + '_' + idt_cat + '_' + idt_que;
                            string nro_quest = linha_cat["ordem"].ToString() + '.' + linha_quest["ordem"].ToString();

                            switch (questao.Tpo_que)
                            {
                                case "A":
                                    {

                                        TextBox tb = (TextBox)cdc.FindControlRecursive(Page.Master, "tb_" + idq);
                                        CRespostasAbertas resp = new CRespostasAbertas();
                                        resp.Idt_qst = _idt_qst;
                                        resp.Idt_tiq = idt_tiq;
                                        resp.Idt_cat = idt_cat;
                                        resp.Idt_que = idt_que;
                                        resp.Resposta = tb.Text;
                                        resp.Inserir(command);
                                        break;
                                    }
                                case "D":
                                    {
                                        CheckBoxList cbl = (CheckBoxList)cdc.FindControlRecursive(Page.Master, "cbl_" + idq);
                                        foreach (ListItem item in cbl.Items)
                                        {
                                            if (item.Selected)
                                            {
                                                CRespostasMultiplas resp = new CRespostasMultiplas();
                                                resp.Idt_qst = _idt_qst;
                                                resp.Idt_tiq = idt_tiq;
                                                resp.Idt_cat = idt_cat;
                                                resp.Idt_que = idt_que;
                                                resp.Seq_opcao = item.Value;
                                                resp.Inserir(command);
                                            }
                                        }
                                        break;
                                    }
                                case "U":
                                    {
                                        RadioButtonList rbl = (RadioButtonList)cdc.FindControlRecursive(Page.Master, "rbl_" + idq);
                                        CRespostasMultiplas resp = new CRespostasMultiplas();
                                        resp.Idt_qst = _idt_qst;
                                        resp.Idt_tiq = idt_tiq;
                                        resp.Idt_cat = idt_cat;
                                        resp.Idt_que = idt_que;
                                        resp.Seq_opcao = rbl.SelectedValue;
                                        resp.Inserir(command);
                                        break;
                                    }
                                case "C":
                                    {
                                        DropDownList ddl = (DropDownList)cdc.FindControlRecursive(Page.Master, "ddl_" + idq);
                                        CRespostasMultiplas resp = new CRespostasMultiplas();
                                        resp.Idt_qst = _idt_qst;
                                        resp.Idt_tiq = idt_tiq;
                                        resp.Idt_cat = idt_cat;
                                        resp.Idt_que = idt_que;
                                        resp.Seq_opcao = ddl.SelectedValue;
                                        resp.Inserir(command);
                                        break;
                                    }
                                default:
                                    break;
                            }
                        }
                    }
                    command.Transaction.Commit();
                }
                catch (Exception)
                {
                    command.Transaction.Rollback();
                } 
                if (Session["RetornoQuest"] != null)
                {
                    Response.Redirect(Session["RetornoQuest"].ToString());
                }
                else
                {
                    Response.Redirect("~/Logged.aspx");
                }
            }
            if (_op.Equals("C"))
            {
                if (Session["RetornoQuest"] != null)
                {
                    Response.Redirect(Session["RetornoQuest"].ToString());
                }
                else
                {
                    Response.Redirect("~/Logged.aspx");
                }
            }
        }
    }
}
